<!DOCTYPE HTML>
<html class="butler">
<head>
    <title>科寓共享家</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>

    <%
    let houseTypeMap = {};
    let tempArray1 = data.houseType;
    for(let idx of tempArray1.keys()){
        houseTypeMap[tempArray1[idx].code] = tempArray1[idx].name;
    }

    let bedTypeMap = {};
    let tempArray2 = data.bedType;
    for(let idx of tempArray2.keys()){
        bedTypeMap[tempArray2[idx].code] = tempArray2[idx].name;
    }

    let payCycleMap = {};
    let tempArray3 = data.payCycleArray;
    for(let idx of tempArray3.keys()){
        payCycleMap[tempArray3[idx].code] = tempArray3[idx].name;
    }

    let payCycleSelect = [];
    let payCycleArray = data.payCycleArray;
    for(let idx of payCycleArray.keys()){
        let item = {};
        item.title = payCycleArray[idx].name;
        item.value = payCycleArray[idx].code;
        payCycleSelect.push(item);
    }
    console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
    console.log(payCycleSelect);


    %>

</head>
<body>
<div class="block fluid">
    <h3>
        <span class="stateIcon"></span>
        合同信息
    </h3>
    <% if(data.user!=undefined){%>
    <% if(  data.status == 2){ %>
    <hr>
    <p>
        <span class="label">合同编码</span>
        <span><%= data.code %></span>
    </p>
    <% } %>
</div>
<% if(data.status == 2){ %>
<div class="block fluid">

    <p>
        <span class="label">房屋名称</span>
        <span><%= data.house.name %></span>
    </p>
    <p>
        <span class="label">房号</span>
        <span><%= data.house.houseNumber %></span>
    </p>
    <p>
        <span class="label">房型</span>
        <span><%= houseTypeMap[data.house.houseType] %></span>
    </p>
    <p>
        <span class="label">床型</span>
        <span>
            <% for(let i = 0 ; i < data.house.beds.length ; i++){
//                if (data.house.beds[i].bedType == 'BED01') {
//                    data.house.beds[i].bedType = '单人床'
//
//                } else {
//                    data.house.beds[i].bedType = '双人床'
//                }
            %>
            <%= bedTypeMap[data.house.beds[i].bedType] %>
            <% } %>
        </span>
    </p>
</div>
<% } %>
<% if(data.status != 2){ %>
<div class="block fluid">
    <p>
        <span class="label">合同编码</span>
        <input type="text" id="contractCode" style="border: hidden">
    </p>

    <p>
        <span class="label">房屋名称</span>
        <span><%= data.name %></span>
    </p>
    <p>
        <span class="label">房号</span>
        <span><%= data.houseNumber %></span>
    </p>
    <p>
        <span class="label">房型</span>
        <span><%= data.houseTypeName %></span>
    </p>
    <p>
        <span class="label">物业费</span>
        <span><%= data.propertyFee %></span>元
    </p>
    <p>
        <span class="label">床型</span>
        <span>
            <% for(let i = 0 ; i < data.beds.length ; i++){
//                if (data.beds[i].bedType == 'BED01') {
//                    data.beds[i].bedType = '单人床'
//
//                } else {
//                    data.beds[i].bedType = '双人床'
//                }
            %>
            <%= bedTypeMap[data.beds[i].bedType] %>
            <% } %>
        </span>
    </p>
</div>
<% } %>
<%}else{%>
    <div class="block fluid">
    <p>
        <span class="label">合同编码</span>
        <input type="text" id="contractCode" style="border: hidden">
    </p>

    <p>
        <span class="label">房屋名称</span>
        <span><%= data.name %></span>
    </p>
    <p>
        <span class="label">房号</span>
        <span><%= data.houseNumber %></span>
    </p>
    <p>
        <span class="label">房型</span>
        <span><%= data.houseTypeName %></span>
    </p>
    <p>
        <span class="label">物业费</span>
        <span><%= data.propertyFee %></span>元
    </p>
    <p>
        <span class="label">床型</span>
        <span>
            <% for(let i = 0 ; i < data.beds.length ; i++){
//                if (data.beds[i].bedType == 'BED01') {
//                    data.beds[i].bedType = '单人床'
//
//                } else {
//                    data.beds[i].bedType = '双人床'
//                }
            %>
            <%= bedTypeMap[data.beds[i].bedType] %>
            <% } %>
        </span>
    </p>
</div>
<% } %>

<% if(data.user!=undefined){%>
<% if(  data.status == 2){ %>
<div class="block fluid">
    <p>
        <span class="label">会员名称</span>
        <span><%= data.user.name %></span>
    </p>
    <p>
        <span class="label">身份证号</span>
        <span><%= data.user.idCard %></span>
    </p>
    <p>
        <span class="label">联系方式</span>
        <span><%= data.user.phone %><a class="telIcon" href="tel:<%= data.user.phone %>"></a></span>
    </p>
</div>
<div class="block fluid">
    <p>
        <span class="label">合同月租</span>
        <span><%= data.rental %></span>
    </p>
    <p>
        <span class="label">押金</span>
        <span><%= data.pledge %></span>
    </p>
    <p>
        <span class="label">租房时间</span>
        <span><%= data.startDate.split(' ')[0] %> 至 <%= data.endDate.split(' ')[0] %></span>
    </p>
    <p>
        <span class="label">付款周期</span>
        <span><%= payCycleMap[data.payCycle] %></span>
    </p>
</div>
<div class="block fluid">
    <p>
        <span class="label">初始水表</span>
        <span><%= data.water %></span>
    </p>
    <p>
        <span class="label">初始电表</span>
        <span><%= data.electric %></span>
    </p>
    <p>
        <span class="label">初始燃气</span>
        <span><%= data.gas %></span>
    </p>
</div>
<div class="block fluid">
    <p>
        <span class="label">下次付款</span>
        <span><%= data.nextPayDate %></span>
    </p>
    <p>
        <span class="label">电视费</span>
        <span><%= data.tvFee %></span>
    </p>
    <p>
        <span class="label">其他费用</span>
        <span><%= data.otherFee %></span>
    </p>
    <p>
        <span class="label">其他约定</span>
        <span><%= data.otherPlan %></span>
    </p>
    <a class="weui_btn weui_btn_primary" href="contractDetail?id=<%=data.id%>">详情</a>
</div>
<% }else{ %>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">身份证号</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input " placeholder="请输入客户身份证" name="idcard" id="idcard">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">会员名称</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input " type="text" disabled="disabled" name="idcard" id="vipname" style="color: #000000;background-color: #FFFFFF">
        </div>

        <!--<span class="label">会员名称</span>-->
        <!--<span id="vipname"></span>-->
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">合同月租</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入月租金" value="<%= data.monthly %>" id="rental">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">押金</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入押金" id="pledge">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">入住时间</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" placeholder="请选择入住时间" id="startTime">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">退房时间</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" placeholder="请选择退房时间" id="endTime">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">付款周期</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" id="selections" placeholder="请选择付款周期">
        </div>
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">电视费</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入电视费" id="tvFee">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">其他费用</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入其他费用" id="otherFee">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">其他约定</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="text" placeholder="请输入其他约定" id="otherPlan">
        </div>
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始水表</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入水表数字" id="water">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始电表</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入电表数字" id="electric">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始燃气</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入燃气表数字" id="gas">
        </div>
    </div>
</div>
<a href="javascript:void(0);" class="weui_btn weui_btn_disabled weui_btn_default " id="sub">提交合同</a>
<% } %>
<%}else{%>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">身份证号</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input " placeholder="请输入客户身份证" name="idcard" id="idcard">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">会员名称</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input " type="text" disabled="disabled" name="idcard" id="vipname" style="color: #000000;background-color: #FFFFFF">
        </div>

        <!--<span class="label">会员名称</span>-->
        <!--<span id="vipname"></span>-->
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">合同月租</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入月租金" value="<%= data.monthly %>" id="rental">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">押金</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入押金" id="pledge">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">入住时间</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" placeholder="请选择入住时间" id="startTime">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">退房时间</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" placeholder="请选择退房时间" id="endTime">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">付款周期</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" id="selections" placeholder="请选择付款周期">
        </div>
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">电视费</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入电视费" id="tvFee">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">其他费用</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入其他费用" id="otherFee">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">其他约定</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="text" placeholder="请输入其他约定" id="otherPlan">
        </div>
    </div>
</div>
<div class="weui_cells weui_cells_form">
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始水表</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入水表数字" id="water">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始电表</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入电表数字" id="electric">
        </div>
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">初始燃气</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="number" placeholder="请输入燃气表数字" id="gas">
        </div>
    </div>
</div>
<a href="javascript:void(0);" class="weui_btn weui_btn_disabled weui_btn_default " id="sub">提交合同</a>
<% } %>
<% include common/footer.ejs %>

<script>
    'use strict';
    $(document).ready(function () {
        Dolphin.ajax({
            url: CTX + '/api/509cb2a123d2466b9641232e025609c4',
            type: Dolphin.requestMethod.POST,
            forceUrl: 'false',
            onSuccess: function (reData) {
                contractCode = reData.value.code;
                $('#contractCode').val(contractCode);
            }
        });
    })
    let cardCon, startTime, endTime, rental = $('#rental').val(), pledge, payCircle, water, electric, gas, flag,tvFee,otherFee;
    let sendDate = {},userId;
    let contractCode;
    let herf = window.location.href;
    let houseId = herf.substring(herf.indexOf('houseId=')).split('=')[1];
    let pkid = herf.substring(herf.indexOf('pkid='), herf.indexOf('&&houseId=')).split('=')[1];

    function isCardNo(card) {
        var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        return pattern.test(card);
    };

    let aaa = [ { title: '一月一付', value: '1' },
        { title: '一季度一付', value: '3' },
        { title: '半年一付', value: '6' },
        { title: '一年一付', value: '12' } ];


    $("#selections").select({
        title: "选择付款周期",
        items: JSON.parse('<%= JSON.stringify(payCycleSelect); %>'.replace(/&#34;/g, '"') || "{}")
    });

    $('#idcard').blur(function () {
        let idNo = $('#idcard').val();
        cardCon = isCardNo(idNo);
        console.log(idNo);
        if (!cardCon) {
            $('#idcard').addClass('weui_cell_warn');
            $.toptip('身份证格式不正确', 1500, 'error');
            $('#vipname').val('')
        } else {
            $('#idcard').removeClass('weui_cell_warn');
            Dolphin.ajax({
                url: CTX + '/api/8f7def63cc5845468b2c65a69abb0664',
                type: 'post',
                data: Dolphin.json2string({
                    properties: {
                        "idCard": idNo
                    }
                }),
                forceUrl: 'true',
                onSuccess: function (data) {
                    if (data.success) {
                        if (data.rows.length != 0) {
                            console.log(data.rows[0].name);
                            userId=data.rows[0].id;
                            $('#vipname').val(data.rows[0].name);
                            $('#vipname').attr('disabled', 'disabled');

                        } else {
                            $.toptip('请先注册会员', 'warning');
                            $('#vipname').removeAttr('disabled');
                            cardCon = false;
                        }

                    }
                }
            });

        }
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#startTime').change(function () {
        startTime = $('#startTime').val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }


    });

    $('#endTime').change(function () {
        endTime = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    })

    $('#startTime').click(function () {
        if ($('#endTime').val()) {
            $('#endTime').change(
                function () {
                    endTime = $('#endTime').val()
                }
            )
            $("#endTime").calendar({
//                minDate: new Date(),
                onClose: function () {
                    console.log();
                    console.log('---');
                    $("#startTime").calendar({
                        maxDate: endTime,
                        value: [endTime]
                    });
                }
            });
        }
        else {
            $('#startTime').calendar({
//                    minDate: new Date(),
                    onClose: function () {
                        console.log();
                        console.log('---');
                        $("#endTime").calendar({
                            minDate: startTime,
                            value: [startTime]
                        });
                    }
                }
            );
        }
    });

    $('#endTime').click(function () {
        if ($('#startTime').val()) {
            $("#endTime").calendar({
                minDate: $('#startTime').val(),
            });
        }
        else {
            $('#endTime').calendar({
//                    minDate: new Date(),
                    onClose: function () {
                        console.log();
                        console.log('---');
                        $("#startTime").calendar({
                            maxDate: $('#endTime').val(),
                            value: [$('#endTime').val()]
                        });
                    }
                }
            );
        }
    });

    $('#rental').change(function () {
        rental = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#pledge').change(function () {
        pledge = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#selections').change(function () {
        payCircle = $(this).data("values");
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#water').change(function () {
        water = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#electric').change(function () {
        electric = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });
    $('#tvFee').change(function () {
        tvFee = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });
    $('#otherFee').change(function () {
        otherFee = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    $('#gas').change(function () {
        gas = $(this).val();
        flag = Boolean(cardCon && startTime && endTime && rental && pledge && payCircle && water && electric && gas && tvFee && otherFee);
        if (flag) {
            getBtn();
        } else {
            removeBtn();
        }
    });

    function getBtn() {
        $('#sub').attr('class', 'weui_btn weui_btn_primary')
    };

    function removeBtn() {
        $('#sub').attr('class', 'weui_btn weui_btn_disabled weui_btn_default')
    };

    $('#sub').click(function () {
        let condition = $(this).attr('class');
        let sendData = {};
        let code = contractCode;
        if (condition == 'weui_btn weui_btn_disabled weui_btn_default ') {
            $.toptip('请先完成合同信息！', 'error');
        } else {
            sendData = {
                house: {},
                user:{}
            };
            sendData.code = code;
            sendData.name = code;
            sendData.startDate = formatDate(startTime);
            sendData.endDate = formatDate(endTime);
            sendData.rental = parseFloat(rental);
            sendData.pledge = parseFloat(pledge);
            sendData.water = parseFloat(water);
            sendData.electric = parseFloat(electric);
            sendData.gas = parseFloat(gas);
            sendData.tvFee = parseFloat(tvFee);
            sendData.otherFee = parseFloat(otherFee);
            sendData.payCycle = parseFloat(payCircle);
            sendData.payStatus = 0;
            sendData.generateBill = false;
            sendData.house.id = houseId;

            sendData.user.id=userId;

            console.log(sendData);
            Dolphin.ajax({
                url: CTX + '/api/509cb2a903d2466b9645432e025609c4',
                type: 'put',
                data: Dolphin.json2string(sendData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    //生成合同生成第一次账单
                    Dolphin.ajax({
                        url:CTX +  '/api/abadf0c9542b4ba2aaacc819055d7d9c',
                        type:'put',
                        data: Dolphin.json2string({"id" : data.value.id }),
                        forceUrl: 'true',
                        onSuccess: function (reData) {
                            $.toptip('提交成功', 2000, 'success');
                            setTimeout(function () {
//                        location.href = CTX + "view/butlerIndex";
                                location.href = CTX +'/view/butlerIndex';
                            }, 1500)
                        }
                    });
                }
            });
        }
    });

    function add0(m) {
        return m < 10 ? '0' + m : m
    };

    function formatDate(needTime) {
        //needTime是整数，否则要parseInt转换
        var time = new Date(needTime);
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }


</script>

</body>
</html>